<?php

function dsp_banner($v_banner_type, $v_banner_link, $v_banner_image_file, $v_style="") {
	$v_retval = "";
	switch ($v_banner_type) {
		case 0;
			if ($v_banner_link) {
				$v_retval .= '<a target="_blank" href="' . $v_banner_link . '"><img ' . $v_style . ' src="' . URL . $v_banner_image_file . '" border="0"></a>';
			} else {
				$v_retval .= '<img ' . $v_style . ' src="' . URL . $v_banner_image_file . '" border="0">';
			}
			break;
		case 1;
			if ($v_banner_link) {
				$v_retval .= '<a target="_blank" href="' . $v_banner_link . '"><img ' . $v_style . ' src="' . URL . $v_banner_image_file . '" border="0"></a>';
			} else {
				$v_retval .= '<img ' . $v_style . ' src="' . URL . $v_banner_image_file . '" border="0">';
			}
			break;
		case 2;
			if ($v_banner_link) {
				$v_retval .= '
					<a target="_blank" href="' . $v_banner_link . ' ">
					<object width="100%" height="100%" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0">
							<param name="movie" value="' . URL . $v_banner_image_file . '">
							<param name="quality" value="high">
							<param name="wmode" value="transparent">
							<param name="play" value="true">
							<embed quality="high" play="true" width="100%" height="100%" wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" src="' . IMGURL . $v_banner_image_file . '" type="application/x-shockwave-flash"></embed>
					</object>
					</a>';
			} else {
				$v_retval .= '
					<object width="100%" height="100%" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0">
							<param name="movie" value="' . URL . $v_banner_image_file . '">
							<param name="quality" value="high">
							<param name="wmode" value="transparent">
							<param name="play" value="true">
							<embed quality="high" play="true" width="100%" height="100%" wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" src="' . IMGURL . $v_banner_image_file . '" type="application/x-shockwave-flash"></embed>
					</object>';
			}
			break;
	}
	return $v_retval;
}

function get_banner_image($p_banner_id) {
    $rs = mysql_query("
		SELECT 
			BANNER_IMAGE
		FROM TB_BANNER
		WHERE BANNER_ID = '" . $p_banner_id . "'	
	") or die(mysql_error());
    return $rs;
}

function get_header_banner_image($p_category) {

    $v_where_clause = " AND BANNER_CATEGORY =" . $p_category . " AND BANNER_STATUS >0";
    $rs_all_banner = mysql_query("
		SELECT 
			BANNER_ID
			,BANNER_CATEGORY
			,BANNER_TITLE
			,BANNER_LINK
			,BANNER_TIME
			,BANNER_STATUS
			,BANNER_TYPE
		FROM TB_BANNER
		WHERE 1 " . $v_where_clause . "	
		ORDER BY BANNER_STATUS DESC, BANNER_ID ASC
	") or die(mysql_error());

    $v_retval = "";
    $v_link = "";
    while ($arr_banner = mysql_fetch_array($rs_all_banner, MYSQL_NUM)) {
        $v_banner_id = $arr_banner[0];
        $v_banner_category = $arr_banner[1];
        $v_banner_title = $arr_banner[2];
        $v_banner_link = $arr_banner[3];
        $v_banner_time = $arr_banner[4];
        $v_banner_status = $arr_banner[5];
        $v_banner_type = $arr_banner[6];
        switch ($v_banner_type) {
            case 0;
                $v_banner_image_file = "lib/images/banner/" . $v_banner_id . ".jpg";
                break;
            case 1;
                $v_banner_image_file = "lib/images/banner/" . $v_banner_id . ".gif";
                break;
            case 2;
                $v_banner_image_file = "lib/images/banner/" . $v_banner_id . ".swf";
                break;
        }
        if (!file_exists($v_banner_image_file)) {
            $rs_banner_image_file = get_banner_image($v_banner_id);
            $arr_banner_image_file = mysql_fetch_array($rs_banner_image_file, MYSQL_NUM);
            $v_banner_image_str = $arr_banner_image_file[0];
            if ($v_banner_image_str) {
                $handle = fopen($v_banner_image_file, "w");
                fwrite($handle, $v_banner_image_str);
            }
        }

        $v_retval .= $v_banner_image_file . ",";
        $v_link .= $v_banner_link . ",";
    }
    $v_retval = substr($v_retval, 0, (strlen($v_retval) - 1));
    $v_link = substr($v_link, 0, (strlen($v_link) - 1));
    return $v_retval . ";" . $v_link;
}

function dsp_slider_banner($p_category=2, $p_attribute = "") {

    $v_where_clause = " AND BANNER_CATEGORY =" . $p_category . " AND BANNER_STATUS >0 ";

    $rs_all_banner = mysql_query("
		SELECT 
			BANNER_ID
			,BANNER_CATEGORY
			,BANNER_TITLE
			,BANNER_LINK
			,BANNER_TIME
			,BANNER_STATUS
			,BANNER_TYPE
		FROM TB_BANNER
		WHERE 1 " . $v_where_clause . "	
		ORDER BY BANNER_STATUS DESC, BANNER_ID ASC
	") or die(mysql_error());
    $v_retval = "";

    while ($arr_banner = mysql_fetch_array($rs_all_banner, MYSQL_NUM)) {
        $v_banner_id = $arr_banner[0];
        $v_banner_category = $arr_banner[1];
        $v_banner_title = $arr_banner[2];
        $v_banner_link = $arr_banner[3];
        $v_banner_time = $arr_banner[4];
        $v_banner_status = $arr_banner[5];
        $v_banner_type = $arr_banner[6];
        switch ($v_banner_type) {
            case 0;
                $v_banner_image_file = "lib/images/banner/" . $v_banner_id . ".jpg";
                break;
            case 1;
                $v_banner_image_file = "lib/images/banner/" . $v_banner_id . ".gif";
                break;
            case 2;
                $v_banner_image_file = "lib/images/banner/" . $v_banner_id . ".swf";
                break;
        }
        if (!file_exists($v_banner_image_file)) {
            $rs_banner_image_file = get_banner_image($v_banner_id);
            $arr_banner_image_file = mysql_fetch_array($rs_banner_image_file, MYSQL_NUM);
            $v_banner_image_str = $arr_banner_image_file[0];
            if ($v_banner_image_str) {
                $handle = fopen($v_banner_image_file, "w");
                fwrite($handle, $v_banner_image_str);
            }
        }
        $v_retval .= "<li " . $p_attribute . " >";
        $v_retval .= dsp_banner($v_banner_type, $v_banner_link, $v_banner_image_file, 'width="952" height="346"');
        $v_retval .= "</li>";
    }
    return $v_retval;
}

function dsp_ad_banner($p_category=2, $p_attribute = "") {

    $v_where_clause = " AND BANNER_CATEGORY =" . $p_category . " AND BANNER_STATUS >0 ";

    $rs_all_banner = mysql_query("
		SELECT 
			BANNER_ID
			,BANNER_CATEGORY
			,BANNER_TITLE
			,BANNER_LINK
			,BANNER_TIME
			,BANNER_STATUS
			,BANNER_TYPE
		FROM TB_BANNER
		WHERE 1 " . $v_where_clause . "	
		ORDER BY BANNER_STATUS DESC, BANNER_ID ASC
	") or die(mysql_error());
    $v_retval = "";

    while ($arr_banner = mysql_fetch_array($rs_all_banner, MYSQL_NUM)) {
        $v_banner_id = $arr_banner[0];
        $v_banner_category = $arr_banner[1];
        $v_banner_title = $arr_banner[2];
        $v_banner_link = $arr_banner[3];
        $v_banner_time = $arr_banner[4];
        $v_banner_status = $arr_banner[5];
        $v_banner_type = $arr_banner[6];
        switch ($v_banner_type) {
            case 0;
                $v_banner_image_file = "lib/images/banner/" . $v_banner_id . ".jpg";
                break;
            case 1;
                $v_banner_image_file = "lib/images/banner/" . $v_banner_id . ".gif";
                break;
            case 2;
                $v_banner_image_file = "lib/images/banner/" . $v_banner_id . ".swf";
                break;
        }
        if (!file_exists($v_banner_image_file)) {
            $rs_banner_image_file = get_banner_image($v_banner_id);
            $arr_banner_image_file = mysql_fetch_array($rs_banner_image_file, MYSQL_NUM);
            $v_banner_image_str = $arr_banner_image_file[0];
            if ($v_banner_image_str) {
                $handle = fopen($v_banner_image_file, "w");
                fwrite($handle, $v_banner_image_str);
            }
        }
        $v_retval .= "<div " . $p_attribute . " >";
        $v_retval .= dsp_banner($v_banner_type, $v_banner_link, $v_banner_image_file);
        $v_retval .= "</div>";
    }
    return $v_retval;
}

function dsp_ad_banner_bottom($p_category=2, $p_attribute = "") {

    $v_where_clause = " AND BANNER_CATEGORY =" . $p_category . " AND BANNER_STATUS >0 ";

    $rs_all_banner = mysql_query("
		SELECT 
			BANNER_ID
			,BANNER_CATEGORY
			,BANNER_TITLE
			,BANNER_LINK
			,BANNER_TIME
			,BANNER_STATUS
			,BANNER_TYPE
		FROM TB_BANNER
		WHERE 1 " . $v_where_clause . "	
		ORDER BY BANNER_STATUS DESC, BANNER_ID ASC
	") or die(mysql_error());
    $v_retval = "";
    $v_ince = 1;
    while ($arr_banner = mysql_fetch_array($rs_all_banner, MYSQL_NUM)) {
        $v_banner_id = $arr_banner[0];
        $v_banner_category = $arr_banner[1];
        $v_banner_title = $arr_banner[2];
        $v_banner_link = $arr_banner[3];
        $v_banner_time = $arr_banner[4];
        $v_banner_status = $arr_banner[5];
        $v_banner_type = $arr_banner[6];
        switch ($v_banner_type) {
            case 0;
                $v_banner_image_file = "lib/images/banner/" . $v_banner_id . ".jpg";
                break;
            case 1;
                $v_banner_image_file = "lib/images/banner/" . $v_banner_id . ".gif";
                break;
            case 2;
                $v_banner_image_file = "lib/images/banner/" . $v_banner_id . ".swf";
                break;
        }
        if (!file_exists($v_banner_image_file)) {
            $rs_banner_image_file = get_banner_image($v_banner_id);
            $arr_banner_image_file = mysql_fetch_array($rs_banner_image_file, MYSQL_NUM);
            $v_banner_image_str = $arr_banner_image_file[0];
            if ($v_banner_image_str) {
                $handle = fopen($v_banner_image_file, "w");
                fwrite($handle, $v_banner_image_str);
            }
        }
        if ($v_ince == 2)
            $p_attribute = "style='float: left ;padding-left:12px'";
        $v_retval .= "<div " . $p_attribute . " >";
        $v_retval .= dsp_banner($v_banner_type, $v_banner_link, $v_banner_image_file, 'width="480" height="160"');
        $v_retval .= "</div>";
        $v_ince++;
    }
    return $v_retval;
}

?>
